package com.bulletelc.apollopre.module.main.center.invoice.list;

import android.util.Log;

import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.InvoiceBean;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.bulletelc.apollopre.utils.MyUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/29
 * desc  : TODO
 */

public class InvoicePresenter extends BasePresenter<InvoiceContract.View> implements InvoiceContract.Presenter {
    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public void getInvoiceList() {
        getInvoiceListImp();
    }

    @Override
    public void delInvoice(int invoiceId,int position) {
        delInvoiceImp(invoiceId,position);
    }

    @Override
    public void setDefault(int invoiceId) {
        setDefaultImp(invoiceId);
    }

    /**
     * 获取发票列表
     */
    private void getInvoiceListImp() {
        OkGo.<String>get(NetWorkPath.GET_USER_INVOICE)
                .headers(NetWorkPath.HEADER_KEY_AUTH, MyUtil.getAuthToken())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseInvoiceList(response);
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });
    }

    /**
     * 解析发票数据
     *
     * @param response
     */
    private void parseInvoiceList(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject obj = new JSONObject(response.body().toString());
                JSONObject metaObj = new JSONObject(obj.getString("meta"));
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(metaObj.getString("code"))) {
                    Gson gson = new Gson();
                    Type type=new TypeToken<List<InvoiceBean>>(){}.getType();
                    List<InvoiceBean> invoiceBeans = gson.fromJson(obj.getJSONArray("data").toString(), type);
                    if (isViewAttached()) {
                        getView().onSuccessInvoiceList(invoiceBeans);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailInvoiceList(metaObj.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if (isViewAttached()) {
                    getView().onErrorInvoiceList("解析数据有误");
                }
            }
        } else {
            if (isViewAttached()) {
                getView().onFailInvoiceList(response.body().toString());
            }
        }
    }

    /**
     * 删除发票
     * @param invoiceId
     */
    private void delInvoiceImp(int invoiceId,int position){
        OkGo.<String>post(NetWorkPath.GET_USER_INVOICE_DELETE + "?id=" + invoiceId)
                .headers(NetWorkPath.HEADER_KEY_AUTH, MyUtil.getAuthToken())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        //LogUtil.i(response.body().toString());
                        parseDelInvoice(response);

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorDelInvoice(response.getException().toString());
                        }
                    }
                });
    }

    /**
     *
     * @param response
     */
    private void parseDelInvoice(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject obj = new JSONObject(response.body().toString());
                JSONObject objMeta = new JSONObject(obj.getString("meta"));
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(objMeta.getString("code"))) {
                    if (isViewAttached()) {
                        getView().onSuccessDelInvoice();
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailDelInvoice(objMeta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if (isViewAttached()) {
                    getView().onErrorDelInvoice(e.getMessage().toString());
                }
            }
        } else {
            if (isViewAttached()) {
                getView().onFailDelInvoice(response.body().toString());
            }
        }
    }

    /**
     * 设置为默认发票
     * @param invoiceId
     */
    private void setDefaultImp(int invoiceId) {
        OkGo.<String>post(NetWorkPath.POST_USER_INVOICE_IS_DEFAULT + "?id=" + invoiceId)
                .headers(NetWorkPath.HEADER_KEY_AUTH, MyUtil.getAuthToken())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        //LogUtil.i(response.body().toString());
                        parseSetDefault(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorSetDefault(response.getException().toString());
                        }
                    }
                });
    }

    /**
     * 解析设置默认返回的数据
     * @param response
     */
    private void parseSetDefault(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject obj = new JSONObject(response.body().toString());
                JSONObject objMeta = new JSONObject(obj.getString("meta"));
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(objMeta.getString("code"))) {
                    if (isViewAttached()) {
                        getView().onSuccessSetDefault();
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailSetDefault(objMeta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if (isViewAttached()) {
                    getView().onErrorSetDefault(e.getMessage().toString());
                }
            }
        } else {
            if (isViewAttached()) {
                getView().onFailSetDefault(response.body().toString());
            }
        }
    }

}
